package com.scrx.ymg.service.head;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.scrx.ymg.dao.agent.IAgentInfoDao;
import com.scrx.ymg.dao.direct.IDirectDao;
import com.scrx.ymg.dao.head.IHeadDao;
import com.scrx.ymg.dao.product.IProductDao;
import com.scrx.ymg.dao.seckill.ISeckillDao;
import com.scrx.ymg.dao.supermarket.ISuperMarketDao;
import com.scrx.ymg.entity.AgentDrawings;
import com.scrx.ymg.entity.AgentHot;
import com.scrx.ymg.entity.AgentInfo;
import com.scrx.ymg.entity.AgentMoney;
import com.scrx.ymg.entity.BankAccount;
import com.scrx.ymg.entity.DirectInfo;
import com.scrx.ymg.entity.DirectMoney;
import com.scrx.ymg.entity.DividedInto;
import com.scrx.ymg.entity.GoodsType;
import com.scrx.ymg.entity.HeadEntrepot;
import com.scrx.ymg.entity.HeadMoney;
import com.scrx.ymg.entity.HeadWithdarw;
import com.scrx.ymg.entity.OtherInfo;
import com.scrx.ymg.entity.OtherMoney;
import com.scrx.ymg.entity.SuperMarketEntrepot;
import com.scrx.ymg.entity.SuperMarketInfo;
import com.scrx.ymg.entity.SupermarketMoney;
import com.scrx.ymg.util.BaseUtil;

@Service("headService")
public class HeadServiceImpl implements IHeadService {
    
	@Autowired
	private IHeadDao headao;
	@Autowired
	private IProductDao prodao;
	@Autowired
	private IAgentInfoDao agentDao;
	@Autowired
	private ISeckillDao seckilldao;
	@Autowired
	private ISuperMarketDao superDao;
	@Autowired
	private IDirectDao directdao;
	
	
	public String addGoodsType(GoodsType goodsType) {
		return headao.addParentGoodsType(goodsType);
	}


	public String updateGoodsType(GoodsType goodsType) {
		String msg = "false";
		GoodsType goods=headao.findOne(goodsType.getId());
		if(goods!=null){
			goods.setGoodsName(goodsType.getGoodsName());
			msg=headao.updateGoodsType(goods);
		}
		return msg;
	}


	public String auditProduct(HeadEntrepot head) {
		String msg="false";
		HeadEntrepot oldHead = prodao.selectHeadEntrepotByID(head.getId());
		if(oldHead!=null){
			oldHead.setChecka(head.getChecka());
			msg = prodao.update(oldHead);
		}
		return msg;
	}

	public String deleteHeadProduct(HeadEntrepot head) {
		String msg="false";
		HeadEntrepot oldHead = prodao.selectHeadEntrepotByID(head.getId());
		if(oldHead!=null){
			msg = prodao.delete(oldHead);
		}
		return msg;
	}

	public String checkHotProduct(AgentHot hot) {
		String msg="false";
		AgentHot agentHot = agentDao.getHotProduct(hot.getId());
		if(agentHot!=null){
			agentHot.setChecka(hot.getChecka());
			if(hot.getChecka()==1){//审核通过
				//得到代理商旗下所有超市
				List<SuperMarketInfo> superList=agentDao.getListSuper(agentHot.getAgentId());
				List<SuperMarketEntrepot> entreList=new ArrayList<SuperMarketEntrepot>();
				//每个超市添加爆款
				for (SuperMarketInfo info : superList) {
					SuperMarketEntrepot entre=new SuperMarketEntrepot();
					entre.setAdvertisement(agentHot.getAdvertisement());
					entre.setBarCode(agentHot.getBarCode());
					entre.setChecka(agentHot.getChecka());
					entre.setDiscountPrice(agentHot.getHotMoney());
					entre.setHot(1);
					entre.setInactive1(agentHot.getGoodstype());
					entre.setInactive3(agentHot.getNum());
					entre.setProductBimg(agentHot.getProductBimg());
					entre.setProductDetail(agentHot.getProductDetail());
					entre.setProductName(agentHot.getProductName());
					entre.setProductSimg(agentHot.getProductSimg());
					entre.setPutaway(1);//是否上架
					entre.setSales(0);
					entre.setSuperId(info.getId());
					entre.setRepertory(0);
					entre.setInactive3(agentHot.getNum());
					entre.setAgentHotId(agentHot.getId());
					entre.setSuperDetailImg(agentHot.getHotDetailImg());
					entreList.add(entre);
				}
				msg =agentDao.updateAgentHotAndSuperEntrepot(agentHot,entreList);
			}
			msg=agentDao.updateAgentHot(agentHot);
		}
		return msg;
	}



	public HeadWithdarw selectHeadWithdarw() {

		return agentDao.selectHeadWithdarw();
	}



	public String updateHeadWithdarw(HeadWithdarw head) {
		return agentDao.updateHeadWithdarw(head);
	}



	public String updateAgentInfo(AgentInfo info) {
		AgentInfo old = agentDao.getAgentInfoByID(info.getId());
		old.setPaymentMode(info.getPaymentMode());
		return headao.updateAgentInfo(old);
	}



	public String updateSuperMarketInfo(SuperMarketInfo info) {
		SuperMarketInfo oldinfo = superDao.findByID(info.getId());
		oldinfo.setPaymentMode(info.getPaymentMode());
		return superDao.update(oldinfo);
	}



	public SuperMarketInfo getSuperMarketInfo(SuperMarketInfo info) {
		return superDao.findByID(info.getId());
	}



	public DividedInto selectDividedInto() {

		return headao.selectDividedInto();
	}



	public String updateDividedInto(DividedInto into) {

		return headao.updateDividedInto(into);
	}


	public AgentDrawings getDrawingsById(String transferNo) {

		return headao.getDrawingsById(transferNo);
	}



	public String getOpenIdByType(Integer merchantId, Integer typeId) {
		BankAccount account=null;
            if(typeId==1){
            	account=headao.getBankAccountBySuperId(merchantId);
            }else if(typeId==2){
            	account=headao.getBankAccountByAgentId(merchantId);
            }else if(typeId==3){
            	account=headao.getBankAccountByOtherId(merchantId);
            }else if(typeId==4){
            	account=headao.getBankAccountByDirectId(merchantId);
            }
		   if(account!=null){
			   return account.getIdCard();
		   }
		return null;
	}

	public HeadMoney getHeadMoney() {
		return headao.getHeadMoney();
	}



	public String updateAgentDrawings(AgentDrawings drawing) {
		return headao.updateAgentDrawings(drawing);
	}



	public String rejectTransferPay(AgentDrawings drawing) {
		//1.修改提现申请状态为拒绝
		if(drawing.getStatus()==0){
			drawing.setStatus(2);
			drawing.setCheckTime(BaseUtil.newdate());
		}
		//2.将提现金额退回原处
		Double returnMoney = drawing.getTotalAmount();
		//2.1得到商家资金账户
		int typeId=drawing.getTypeId();
		int merchantId=drawing.getMerchantId();
		Object obj=null;
		if(typeId==1){//超市
			SupermarketMoney money =superDao.findSupermarketMoneyByID(merchantId);
			money.setAccountMoney(money.getAccountMoney()+returnMoney);
			obj=money;
        }else if(typeId==2){//代理商
        	AgentMoney money = agentDao.getAgentMoneyInfo(merchantId);
        	money.setAccountMoney(money.getAccountMoney()+returnMoney);
        	obj=money;
        }else if(typeId==3){//异业
        	OtherMoney money = seckilldao.getOtherMoney(merchantId);
        	money.setAccountMoney(money.getAccountMoney()+returnMoney);
        	obj=money;
        }else if(typeId==4){//直营
        	DirectMoney money = directdao.getDirectMoney(merchantId);
        	money.setAccountMoney(money.getAccountMoney()+returnMoney);
        	obj=money;
        }
		//3.更新平台提现金额
		 HeadMoney headMoney = headao.getHeadMoney();
		 headMoney.setTerraceAccountMoney(headMoney.getTerraceAccountMoney()+returnMoney);
		
		return  headao.updateReturnMoney(drawing,obj,headMoney);
	}



}
